package com.csdj.controller;

import com.csdj.service.InformationsService;
import com.csdj.vo.Informations;
import com.csdj.vo.Replies;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: InformationsController
 * @Author: ZouLi
 * @Description: 咨询控制器
 * @Date: 2022/9/7 17:09
 **/
@Controller
public class InformationsController {
    // 注入service
    @Resource
    private InformationsService informationsService;

    /**
     * @Description: 进入主页
     * @Param [model]
     * @Return java.lang.String
     * @Throws
     **/
    @GetMapping("/home")
    public String home(Model model){
        List<Informations> informationList = informationsService.selectAllInformation();
        model.addAttribute("informationList",informationList);
        return "index";
    }

    /**
     * @Description: 根据资讯id进入资讯内容
     * @Param []
     * @Return java.lang.String
     * @Throws
     **/
    @GetMapping("/inReplies/{id}")
    public String inRepliesByInfoId(@PathVariable Integer id, Model model){
        model.addAttribute("replies",informationsService.selectInformationById(id));
        return "replies";
    }

    @PostMapping("/inReplies/add")
    @ResponseBody
    public Map<String, Object> addReplie(@RequestBody Replies replies){
        HashMap<String, Object> map = new HashMap<>();
        if (informationsService.addReplie(replies)){
            map.put("code",200);
            map.put("msg","添加成功！");
            map.put("content",replies);
        }else{
            map.put("code",300);
            map.put("msg","添加失败！");
        }

        return map;
    }
 }
